package com.banshuibao.bsb_android.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;

/**
 * Created by zyh on 2017/12/16.
 */

public class GradientTextView extends TextView {

    private Context mContext;
    private Rect mTextBound = new Rect();

    public GradientTextView(Context context) {
        super(context);
        mContext = context;
    }

    public GradientTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

//    @Override
//    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
//        super.onLayout(changed, left, top, right, bottom);
//        if (changed)
//            getPaint().setShader(new LinearGradient((right - left) / 4, 0, (right - left)/2, 0,
//                    new int[]{mContext.getResources().getColor(R.color.user_icon_stroke_blue1),
//                            mContext.getResources().getColor(R.color.user_icon_stroke_blue2)},
//                    null, Shader.TileMode.CLAMP));
//    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint mPaint = getPaint();
        String mTipText = getText().toString();
        mPaint.getTextBounds(mTipText, 0, mTipText.length(), mTextBound);
        LinearGradient mLinearGradient = new LinearGradient(0, 0, getMeasuredWidth(), 0,
                new int[]{mContext.getResources().getColor(R.color.user_icon_stroke_blue1),
                           mContext.getResources().getColor(R.color.user_icon_stroke_blue2)},
                null, Shader.TileMode.REPEAT);
        mPaint.setShader(mLinearGradient);
        canvas.drawText(mTipText, getMeasuredWidth() / 2 - mTextBound.width() / 2, getMeasuredHeight() / 2 +   mTextBound.height()/3, mPaint);
    }
}
